#include "common.h"
#include "globalvar.h"
#include "netpack.h"
#include "client.h"
#include "msgfilter.h"

MsgFilterBase::MsgFilterBase( EFilterType et )
{
  eftype=et;
  memset(functbl,0,sizeof(functbl));
}

bool MsgFilterBase::regmsgfunc( uint16 m,msgfunc f )
{
  if(m>sizeofa(functbl))
  {
    STLStringStream ss;
    ss<<"regmsgfunc overflow,msgid="<<m;
    GPrinter->error(ss.str().c_str());
    return false;
  }
  functbl[m]=f;
  return true;
}

void MsgFilterBase::OnRecvNetPacket( Client* client,const NetPacket& recvPack )
{
  if(recvPack.Id()>=sizeofa(functbl))
  {
    GPrinter->error("");
    return;
  }
  msgfunc f=functbl[recvPack.Id()];
  if(f)
  {
    f(client,recvPack);
  }
}

MsgFilterManager::MsgFilterManager()
{
  memset(_filter,0,sizeof(_filter));
  _filter[EFT_PRE]=new MsgFilterPre(EFT_PRE);
  _filter[EFT_LOGIN]=new MsgFilterNormal(EFT_LOGIN);
}

MsgFilterManager::~MsgFilterManager()
{
  for(int i=0;i<sizeofa(_filter);++i)
  {
    Cross::delete_safe(_filter[i]);
  }
}

INetFilter* MsgFilterManager::CreateFilter(int id)
{
  return _filter[id];
}

void MsgFilterManager::Release()
{

}